<template>
  <view class="first-category">
    <view class="list">
      <view class="item" v-for="(item, index) in list" :key="item.id"
        @click="handleFirst(item, index)">
        <view class="x">
          <u--image
              :class="{'active': index === currentIndex}"
              class="img"
              v-if="item.image"
              :lazy-load="true"
              width="100rpx"
              height="100rpx"
              shape="circle"
              :src="baseUrl + item.image.path" />
          <view class="name" :class="{'active': index === currentIndex}">{{item.name}}</view>
        </view>
      </view>
    </view>
    <view class="all" @click="all()">
      <view class="text">全</view>
      <view class="text">部</view>
      <u-icon name="list-dot"></u-icon>
    </view>
    <u-popup :show="showAll" mode="top" :safeAreaInsetTop="true" @close="showAll = false">
      <!-- 留出顶部 -->
      <u-status-bar></u-status-bar>
      <view class="pop">
        <view class="title">全部分类</view>
        <view class="list show">
          <view class="item" v-for="(item, index) in list" :key="item.id"
                @click="handleFirst(item, index, true)">
            <view class="x">
              <u--image
                  :class="{'active': index === currentIndex}"
                  class="img"
                  v-if="item.image"
                  :lazy-load="true"
                  width="100rpx"
                  height="100rpx"
                  shape="circle"
                  :src="baseUrl + item.image.path" />
              <view class="name" :class="{'active': index === currentIndex}">{{item.name}}</view>
            </view>
          </view>
        </view>
        <view class="close">
          <view class="btn" @click="showAll = false">点击收起</view>
        </view>
      </view>
    </u-popup>
  </view>
</template>

<script>
import config from '../../../../config/config'

export default {
  props: ['list'],
  components: {
  },
  computed: {
    useList() {
      // 只使用8个
      return this.list.filter((v, k) => k < 8)
    }
  },
  data() {
    return {
      baseUrl: config.baseUrl,
      currentIndex: 0,
      showAll: false
    }
  },
  methods: {
    // 一级分类
    handleFirst(item, index, close = false) {
      if (close) {
        this.showAll = false
      }
      this.currentIndex = index
      this.$emit('handleFirst', item)
    },
    // 显示全部
    all() {
      this.showAll = true
    }
  },
  created() {

  },
  mounted() {
    // this.$nextTick(() => {
    //   const c = uni.createSelectorQuery().in(this).select('c')
    //   console.log(c)
    //   c.boundingClientRect(rect => {
    //     console.log(rect)
    //   }).exec()
    // })
  },

}
</script>

<style scoped lang="scss">
$height: 160rpx;

.first-category {
  height: $height;
  position: relative;
  //padding: 0 20rpx;
  .all {
    position: absolute;
    display: flex;
    flex-direction: column;
    width: 70rpx;
    height: $height;
    top: 0;
    right: 0;
    align-items: center;
    justify-content: center;
    background: $page_bg_color;
    .text {
      font-size: 24rpx;
    }
  }
  .list {
    //background: red;
    height: $height;
    overflow-x: auto;
    display: flex;
    padding-left: 20rpx;
    padding-right: 80rpx;
    // 全部分类
    &.show {
      padding: 0;
      height: auto;
      display: grid;
      grid-template-columns: 1fr 1fr 1fr 1fr;
      .x {
      }
    }
    .item {
      //margin-right: 30rpx;
      //background: #cccccc;
      .x {
        min-width: 150rpx;
        min-height: 160rpx;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
      }
      .name {
        height: 45rpx;
        line-height: 45rpx;
        //background: white;
        font-size: 24rpx;
        text-align: center;
        //margin-top: 10rpx;
        overflow: hidden;
        margin-top: 10rpx;
        &.active {
          background: $main_color;
          color: white;
          border-radius: 30rpx;
          padding: 0 20rpx;
          display: inline-block;
        }
      }
      .img {
        border: 1rpx solid $page_bg_color;
        border-radius: 50%;
        &.active {
          border-color: $main_color;
        }
      }
    }
  }
}
.pop {
  padding: 50rpx 20rpx 20rpx 20rpx;
  .title {
    font-size: 28rpx;
    font-weight: 700;
    padding-bottom: 30rpx;
  }
  .close {
    display: flex;
    justify-content: center;
    padding-top: 30rpx;
    .btn {
      display: inline-block;
      color: #000000;
      background: #cccccc;
      padding: 10rpx 20rpx;
      border-radius: 10rpx;
      font-size: 24rpx;
    }
  }
}
</style>
